﻿@using Masuit.MyBlogs.Core.Models.DTO
@using Masuit.MyBlogs.Core.Models.Entity
@using Masuit.MyBlogs.Core.Models.ViewModel
@using Masuit.Tools.Core.Net
@using Masuit.MyBlogs.Core.Common
@using Masuit.MyBlogs.Core.Infrastructure.Services.Interface
@using Masuit.MyBlogs.Core.Models.Enum
@using EFCoreSecondLevelCacheInterceptor
@using Masuit.Tools.Models
@using Collections.Pooled
@model Masuit.MyBlogs.Core.Models.ViewModel.HomePageViewModel
@inject ICategoryService CategoryService
@{
    Category cat = ViewBag.Category;
    ViewBag.Title = "分类_" + cat.Path();
    Layout = "~/Views/Shared/_Layout.cshtml";
    var level = cat.Level();
    using var children2 = new PooledList<Category>();
    using var children3 = new PooledList<Category>();
    var parentId = cat.ParentId;
    switch (level) {
        case 1:
            children2.AddRange(cat.Children.Where(c => c.Status == Status.Available).OrderBy(c => c.Id).ToPooledList());
            break;
        case 2:
            children2.AddRange(CategoryService.GetQuery(c => c.Status == Status.Available && c.ParentId == parentId, c => c.Name).Cacheable().ToPooledList());
            children3.AddRange(cat.Children.Where(c => c.Status == Status.Available).OrderBy(c => c.Id).ToPooledList());
            break;
        case 3:
            var topid = cat.Parent.ParentId;
            children2.AddRange(CategoryService.GetQuery(c => c.Status == Status.Available && c.ParentId == topid, c => c.Name).Cacheable().ToPooledList());
            children3.AddRange(CategoryService.GetQuery(c => c.Status == Status.Available && c.ParentId == parentId, c => c.Name).Cacheable().ToPooledList());
            break;
    }
    var alllist = CategoryService.GetQuery(c => c.Status == Status.Available && c.ParentId == null, c => c.Name).Select(c => new{c.Id,c.Name}).Cacheable().ToPooledList();
}
<style>
    .bg-title {
        height: 50vh;
        max-height: 400px;
        position: relative;
        background: url(/Content/images/@(new Random().Next(1,9)).jpg) no-repeat center;
        background-size:cover;
        background-attachment: fixed;
        margin-bottom: 10px;
    }
    .header-content {
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%,-50%);
    }
    .header-content .divider {
        width: inherit;
        height: 20px;
        margin-top: 20px;
        border-top: 3px solid rebeccapurple;
    }
</style>
<div class="container">
    <ol class="cd-breadcrumb triangle">
        <li><a asp-controller="Home" asp-action="Index">首页</a></li>
        <li><a asp-controller="Home" asp-action="Post">文章列表</a></li>
        @if(cat.ParentId>0) {
            if(cat.Parent.ParentId>0) {
                <li><a asp-controller="Home" asp-action="Category" asp-route-id="@cat.Parent.ParentId">@cat.Parent.Parent.Name</a></li>
            }
            <li><a asp-controller="Home" asp-action="Category" asp-route-id="@cat.ParentId">@cat.Parent.Name</a></li>
        }
        <li class="current"><em>@cat.Name</em></li>
    </ol>
</div>
<div class="bg-title">
    <div class="header-content text-center">
        <h1 class="size48">
            @cat.Path()
            @if(CommonHelper.SystemSettings.GetOrAdd("EnableRss", "true") == "true"){
                <a class="btn btn-lg btn-success" asp-controller="Subscribe" asp-action="CategoryRss" asp-route-id="@cat.Id" target="_blank">
                    <i class="icon-rss4"></i>
                </a>
            }
            @if(Context.Request.GetHideCategories().Contains(cat.Id)) {
                <a class="btn btn-lg btn-default" onclick='blockCategory("@cat.Id","@cat.Name")'>
                    <i class="icon-blocked"></i>
                </a>
            }else {
                <a class="btn btn-lg btn-danger" onclick='blockCategory("@cat.Id","@cat.Name")'>
                    <i class="icon-blocked"></i>
                </a>
            }
        </h1>
        <div class="divider"></div>
        <p class="size24">
            @cat.Description
        </p>
    </div>
</div>
<div class="container">
    <p>
        一级分类：
        @foreach (var category in alllist)
        {
            if (cat.Id==category.Id||cat.ParentId==category.Id|| (level==3&&cat.Parent.ParentId == category.Id)) {
                <a class="text-red" asp-controller="Home" asp-action="Category" asp-route-id="@category.Id">@category.Name</a><text> | </text>
            } else {
                <a asp-controller="Home" asp-action="Category" asp-route-id="@category.Id">@category.Name</a><text> | </text>
            }
        }
    </p>
    @if(children2.Count>0) {
        <hr />
        <p>
            <text>二级分类：</text>
            @foreach (var category in children2)
            {
                if (cat.Id==category.Id||cat.ParentId==category.Id) {
                    <a class="text-red" asp-controller="Home" asp-action="Category" asp-route-id="@category.Id">@category.Name</a><text> | </text>
                } else {
                    <a asp-controller="Home" asp-action="Category" asp-route-id="@category.Id">@category.Name</a><text> | </text>
                }
            }
        </p>
    }
    @if(children3.Count>0) {
        <hr />
        <p>
            <text>三级分类：</text>
            @foreach (var category in children3)
            {
                if (cat.Id==category.Id) {
                    <a class="text-red" asp-controller="Home" asp-action="Category" asp-route-id="@category.Id">@category.Name</a><text> | </text>
                } else {
                    <a asp-controller="Home" asp-action="Category" asp-route-id="@category.Id">@category.Name</a><text> | </text>
                }
            }
        </p>
    }
    <hr />
</div>
@{
    var user = Context.Session.Get<UserInfoDto>(SessionKey.UserInfo) ?? new UserInfoDto();
    await Html.RenderPartialAsync(user.IsAdmin ? "_MainContainer_Admin" : "_MainContainer", Model);
}